package com.nix.store;

import com.nix.model.NXTextModel;
import com.nix.model.page.element.EntryType;


/**
 * 
 * 取 CharStorage中间的数据 
 * @author fangliang
 *
 */
public class EntryIteratorImpl implements EntryIterator {

	
	private NXTextModel nxTextModel;
	
	
	private int myCounter;
	private int myLength;
	private byte myType;

	int myDataIndex;
	int myDataOffset;

	private char[] myTextData;
	private int myTextOffset;
	private int myTextLength;

	private byte myControlKind;
	private boolean myControlIsStart;
	
	
	
	public EntryIteratorImpl(int index,NXTextModel nxTextModel) {
		
		this.nxTextModel = nxTextModel;
		
		myLength = nxTextModel.getMyParagraphLengths()[index];
		myDataIndex = nxTextModel.getMyStartEntryIndices()[index];
		myDataOffset = nxTextModel.getMyStartEntryOffsets()[index];
		
		//TODO
		
	}
	
	
	
	
	
	
	
	@Override
	public boolean getControlIsStart() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public byte getControlKind() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public short getFixedHSpaceLength() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getHyperlinkId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public byte getHyperlinkType() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public char[] getTextData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getTextLength() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getTextOffset() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public byte getType() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}

	//TODO
	@Override
	public void next() {

		
		int dataOffset = myDataOffset;
		
		NXCharStorage storage = nxTextModel.getCharStorage();
		
		char[] data = storage.block(myDataIndex);
		
		//处理完了 cache0 开始处理 cahe1了
		if (dataOffset == data.length) {
			data = storage.block(++myDataIndex);
			dataOffset = 0;
		}
		
		byte type = (byte)data[dataOffset];
		if (type == 0) {
			data = storage.block(++myDataIndex);
			dataOffset = 0;
			type = (byte)data[0];
		}
		
		
		myType = type;
		++dataOffset;
		
		
		switch (type) {
		
		//前两位 表征文件的长度
		
		case EntryType.TEXT:
			
			myTextLength =
				((int)data[dataOffset++] << 16) +
				(int)data[dataOffset++];
			myTextData = data;
			myTextOffset = dataOffset;
			dataOffset += myTextLength;
			
			break;

		case EntryType.IMAGE:
			
			//TODO
			
			break;
			
		case EntryType.CONTROL:
			
			short kind = (short)data[dataOffset++];
			myControlKind = (byte)kind;
			myControlIsStart = (kind & 0x0100) == 0x0100;
			
			//TODO
			
			break;
			
			
		case EntryType.FIXED_HSPACE:
			
			//TODO
			break;
		case EntryType.FORCED_CONTROL:
			//TODO
			break;
		default:
			break;
		}
		
		++myCounter;
		myDataOffset = dataOffset;
		
		
		
		
	}

}
